/*
clsquare - closed loop simulation system
Copyright (c) 2004, Neuroinformatics Group, Prof. Dr. Martin Riedmiller,
University of Osnabrueck

Author: Martin Riedmiller

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the <ORGANIZATION> nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*/

#include "valueparser.h"
#include "str2val.h"
#include "democontrol.h"
#include <cstdlib>
#include "statespace.h"
#include "state.h"

#define MAX_NUM_ACTIONS 255

#define VERBOSE(xxx) if(verbose) cout <<__PRETTY_FUNCTION__ <<": "<< xxx<<endl

/**********************************************************************/
/* interface to clsquare                                               */
/**********************************************************************/

bool DemoControl::get_action(const double* observation, double* action)
{
// here we compute a random policy as an example. It randomly outputs action from 0 to 3
  int action_value[action_dim];
  
  
  for(int i =0;i < action_dim;i++) {
    action_value[i] = (int) (actions_in_dim[i]*drand48());
    action[i] = action_def[i*MAX_NUM_ACTIONS+action_value[i]];
  }
  
  vector<double> v;
  vector<double>::iterator it;
  for(int klammer = 0; klammer < observation_dim; klammer++) it = v.insert(it, observation[klammer]);
  StateSpace stsp;
  stsp.init(1);
  stsp.addState(v);
  
  // just for documentation:
  char tmp_string[100];
  sprintf(tmp_string,"Action[0] = %g ", action[0]);
  VERBOSE("decided to act, action= "<<tmp_string);
  
 /* for (int i =0;i < action_dim;i++) {
    action_value[i] = (int) ((int)(2*drand48()-1)*4);
    action[i] = action_def[i*MAX_NUM_ACTIONS+action_value[i]];
  }
  
  // just for documentation:
  char tmp_string[100];
  sprintf(tmp_string,"Action[0] = %g ", action[0]);
  VERBOSE("decided to act, action= "<<tmp_string);
*/
  return true;
}

bool DemoControl::deinit(){
  VERBOSE("finishing");
  return true;
}

void DemoControl::notify_episode_starts(const double* initial_observation){ 
  VERBOSE("Episode starts");
};

void DemoControl::notify_episode_stops(const double* final_observation, double final_reward, bool is_terminal_state){ 
  VERBOSE("Episode stops");
};

bool DemoControl::init(const int observation_dim, const int action_dim, double deltat, const char* fname, const char* chapter)
{
  StateSpace stsp;
  stsp.init(1);
  State st;
  vector<double> v;
  vector<double>::iterator it;
  it = v.insert(it, 1.2);
  st.init(v, 0.0, 0.0, 0.0);
  
  this->observation_dim = observation_dim;
  this->action_dim = action_dim;
  verbose = false;

  bool result = read_options(fname);
  srand48(234);
  VERBOSE("init: got observed_observation_dim: "<<observation_dim<<" and action_dim:"<<action_dim<<" and delta_t: "<<deltat);
  return result;
}

bool DemoControl::read_options(const char * fname) {
  char param[255];

  ValueParser vp(fname,"Controller");
  vp.get("verbose",verbose);
  vp.get("actions",param,255);
  parse_actions(param);
  
  return true;
}

void DemoControl::parse_actions(const char* param)
{    
  const char* str = param;
  double val; 
  action_def = new double[MAX_NUM_ACTIONS*action_dim];  
  actions_in_dim = new int[action_dim];

  for (int i = 0; i<action_dim;i++){  
    strskip(str,"[",str);     
    int j = 0;
    while(str2val(str,val,str)) {     
      action_def[i*MAX_NUM_ACTIONS+j] = val;     
      j++;    
    }       
    actions_in_dim[i] = j;   
    strskip(str,"]",str);
  }	  	      
}


REGISTER_CONTROLLER(DemoControl, "A simple controller for demonstration only.")
